package com.lpgu.jpa.entity;

import lombok.Data;
import org.hibernate.annotations.UpdateTimestamp;
import org.springframework.context.annotation.Primary;
import org.springframework.data.annotation.CreatedBy;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedBy;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import javax.persistence.*;
import java.util.Date;
import java.util.List;

/**
 * @author: lpgu
 * @describe:Customer
 * @createAt: 2022/1/2 20:55
 */
@EntityListeners(AuditingEntityListener.class)
@Data
@Entity
@Table(name = "tb_coustomer")
public class Customer {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Column(name = "cust_address")
    private String customerAddress;

    @Column(name = "cust_name")
    private String customerName;

    //单向关联一对一，fetch 设置懒加载
    //orphanRemoval关联移除，一般在修改的时候使用，一旦把关联胡设置为null或者修改为其他关联的值时，如果设置为true则会会把关联胡删除掉
    //option 限制关联关系是否能为null
    //mpperBy 指定外键由谁来管理，主要用于双向单关联
    @OneToOne(cascade = CascadeType.ALL,fetch = FetchType.LAZY,orphanRemoval = true)
    @JoinColumn(name = "account_id",referencedColumnName ="id" )
    private Acount acount;

    //一般采用多对一，因为这样能够减少update
    @OneToMany(cascade = CascadeType.ALL)
    @JoinColumn(name = "cust_id")
    private List<Message> msgs;

    @ManyToMany(cascade = CascadeType.ALL)

    /**
     * name中间表的名称
     * joinColumns customer 与之关联关系
     * inverseJoinColumns 反向表的关联关系
     */
    @JoinTable(name = "tb_customer_role",
    joinColumns = {@JoinColumn(name = "cid")},
    inverseJoinColumns = {@JoinColumn(name = "rid")})
    private List<Role> roles;

    /**
     * hibernate 乐关锁，其原理是在每次修改胡时候对比版本，修改后加1
     */
    private @Version Long version;

    //下面是审计功能
    @Temporal(TemporalType.TIMESTAMP)
    @UpdateTimestamp
    private Date updateAt;

    @Temporal(TemporalType.TIMESTAMP)
    @CreatedDate
    private Date createAt;

    @Temporal(TemporalType.TIMESTAMP)
    @LastModifiedDate
    private Date lastModifyAt;

    @CreatedBy
    private String createBy;

    @LastModifiedBy
    private String updateBy;
}
